﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace winproject.Models
{
    public class CurrencyDetails
    {
        public int nID { get; set; }
        public string sCurISO { get; set; }
        public string sCurName { get; set; }
    }
    public class HelperModel
    {
        #region Find Currency Details - Filters : ID, or Get ALL Item details
        public ReturnResult getCurrencyDetails(out List<CurrencyDetails> oIDetailList, CurrencyDetails oSearchItem = null)
        {
            ReturnResult oResult = null;
            oIDetailList = null;
            try
            {

                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                if (oWinContext.DatabaseExists())
                {
                    oIDetailList = new List<CurrencyDetails>();
                    if (oSearchItem != null && (!string.IsNullOrEmpty(oSearchItem.sCurISO) || !string.IsNullOrEmpty(oSearchItem.sCurName)))
                    {
                        //search request
                        oIDetailList = oWinContext.Currencies.Where(c => c.ISO == oSearchItem.sCurISO)
                                                                .Select(c => new CurrencyDetails()
                                                                {
                                                                    nID = c.ID,
                                                                    sCurISO = c.ISO,
                                                                    sCurName = c.CurrencyName
                                                                }).ToList<CurrencyDetails>();
                    }
                    else if (oSearchItem != null && (oSearchItem.nID) > 0)
                    {
                        //search request
                        oIDetailList = oWinContext.Currencies.Where(c => c.ID == oSearchItem.nID)
                                                                .Select(c => new CurrencyDetails()
                                                                {
                                                                    nID = c.ID,
                                                                    sCurISO = c.ISO,
                                                                    sCurName = c.CurrencyName
                                                                }).ToList<CurrencyDetails>();
                    }
                    else
                    {
                        oIDetailList = oWinContext.Currencies.Select(c => new CurrencyDetails()
                        {
                            nID = c.ID,
                            sCurISO = c.ISO,
                            sCurName = c.CurrencyName
                        }).ToList<CurrencyDetails>();
                    }

                    //var lsCategories = qCatList.ToList<Category>();
                    string resultJson = JsonConvert.SerializeObject(oIDetailList);
                    if (oIDetailList != null && oIDetailList.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(resultJson))
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = resultJson };
                        }
                    }
                    else
                    {
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = resultJson };
                    }
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        #endregion
    }
}